import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { AuthService } from './auth.service';
import { CreateAuthDto } from './dto/create-auth.dto';
import { UpdateAuthDto } from './dto/update-auth.dto';
import { Public } from 'src/common/public.decorator';
import { JwtService } from "@nestjs/jwt"

@Controller('auth')
export class AuthController {
  constructor(private readonly authService: AuthService,
    private readonly JwtService: JwtService
    ) {}

  @Public()
  @Post("enroll")
  async enroll(@Body() body: any): Promise<any> {
    return this.authService.enroll(body);
  }

  @Public()
  @Post("login")
  async login(@Body() body: any): Promise<any>{
    let res =  await this.authService.login(body);
    if(res.code == 0){
        const token = this.JwtService.sign({
            username: body.username,
            password: body.password,
            role: "USER"
        })
        res.msg = token;
        return res;
    }else{
        return res;
    }
  }
  
}
